package com.example.springboot1.controller;

import com.example.springboot1.entity.User;
import com.example.springboot1.service.Registerservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class RegisterController {
    @Autowired
    Registerservice registerservice;
    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public ModelAndView register(ModelAndView modelAndVie){
        User user = new User();
        modelAndVie.addObject("user",user);
        modelAndVie.setViewName("register");
        return modelAndVie;
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ModelAndView handleregister(ModelAndView modelAndVie,User user){
        try {
            registerservice.registerservice(user);
        }
        catch (Exception e){
            throw new RuntimeException(e);
        }
        modelAndVie.setViewName("login");
        return modelAndVie;
    }
}
